<!DOCTYPE html>
<!--
This is a very basic test to make sure testRunner.setAudioData() works correctly.
It generates a 2 seconds long stereo result @44.1KHz
The left channel will be a 880Hz tone, while the right will be 440Hz.
-->
<html>
  <head>
    <title>
      test-basic.html
    </title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="resources/audit-util.js"></script>
    <script src="resources/audit.js"></script>
    <script src="resources/audio-file-utils.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      let sampleRate = 44100.0;
      let lengthInSeconds = 2;

      function generateSinWave(audioBuffer) {
        let n = audioBuffer.length;
        let channelL = audioBuffer.getChannelData(0);
        let channelR = audioBuffer.getChannelData(1);
        let sampleRate = audioBuffer.sampleRate;

        for (let i = 0; i < n; ++i) {
          channelL[i] = Math.sin(880.0 * 2.0 * Math.PI * i / sampleRate);
          channelR[i] = Math.sin(440.0 * 2.0 * Math.PI * i / sampleRate);
        }
      }

      audit.define(
          {
            label: 'test',
            description:
                'Basic audio test infrastructure: testRunner.setAudioData()'
          },
          (task, should) => {
            if (!window.testRunner) {
              should(!window.testRunner, 'window.testRunner is defined')
                  .beTrue();
              task.done();
              return;
            }

            let context = new AudioContext();
            let audioBuffer = context.createBuffer(
                2, lengthInSeconds * sampleRate, sampleRate);

            generateSinWave(audioBuffer);

            let audioData = createAudioData(audioBuffer);
            testRunner.setAudioData(audioData);

            testRunner.notifyDone();
            task.done();
          });

      audit.run();
    </script>
  </body>
</html>
